package com.ybbase.framework.common.util;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;

/**
 * @author cg
 * @date 2019-12-14 21:38
 * @since cloud2.0
 */
public class BeanUtil<T> {

    /**
     * 将MapToBean
     * @param
     * @return
     * @author cg
     * @date 2019-12-14 21:40
     * @since cloud2.0
    */
    public <T> T mapToObject(Map<String,Object> map, Class<T> clazz) throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        if(clazz==null){
            throw new RuntimeException("Class can't is null");
        }
        if(map==null){
            return null;
        }
        Set<String> keys = map.keySet();
        T t = clazz.newInstance();
        for (String key:keys) {
            if("timestamp".equals(key)){
                continue;
            }
            Field field = clazz.getDeclaredField(key);
            if(key.equals(field.getName())){
                Object value = map.get(key);
                if(value!=null && !"".equals(value.toString().trim())){
                    String[] cid = (String[])value;
                    String strValue = cid[0];
                    if(strValue==null||"".equals(strValue)||strValue.equals("null")){
                        continue;
                    }
                    if((field.getType().equals(int.class)||field.getType().equals(Integer.class))&&
                            (!int.class.equals(value.getClass())||!Integer.class.equals(value.getClass()))){
                        value = Integer.parseInt(strValue);
                    }
                    if((field.getType().equals(byte.class)||field.getType().equals(Byte.class))&&
                            (!byte.class.equals(value.getClass())||!Byte.class.equals(value.getClass()))){
                        value = Byte.valueOf(strValue);
                    }
                    if((field.getType().equals(short.class)||field.getType().equals(Short.class))&&
                            (!short.class.equals(value.getClass())||!Short.class.equals(value.getClass()))){
                        value = Short.valueOf(strValue);
                    }
                    if((field.getType().equals(long.class)||field.getType().equals(Long.class))&&
                            (!long.class.equals(value.getClass())||!Long.class.equals(value.getClass()))){
                        value = Long.valueOf(strValue);
                    }
                    if((field.getType().equals(float.class)||field.getType().equals(Float.class))&&
                            (!float.class.equals(value.getClass())||!Float.class.equals(value.getClass()))){
                        value = Float.valueOf(strValue);
                    }
                    if((field.getType().equals(double.class)||field.getType().equals(Double.class))&&
                            (!double.class.equals(value.getClass())||!Double.class.equals(value.getClass()))){
                        value = Double.valueOf(strValue);
                    }
                    if((field.getType().equals(boolean.class)||field.getType().equals(Boolean.class))&&
                            (!boolean.class.equals(value.getClass())||!Boolean.class.equals(value.getClass()))){
                        value = Boolean.parseBoolean(strValue);
                    }
                    if(field.getType().equals(String.class)&&(!String.class.equals(value.getClass()))){
                        value = strValue;
                    }
                    if((field.getType().equals(Date.class))&&(!Date.class.equals(value.getClass()))){
                        SimpleDateFormat format;
                        if (strValue.length() > 10) {
                            format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        } else {
                            format = new SimpleDateFormat("yyyy-MM-dd");
                        }
                        try {
                            value = format.parse(strValue);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
                if(value!=null){
                    if((field.getType().equals(char.class)||field.getType().equals(Character.class))&&
                            (!char.class.equals(value.getClass())||!Character.class.equals(value.getClass()))){
                        String[] cid = (String[])value;
                        String strValue = cid[0];
                        value = strValue.subSequence(0,strValue.length()).charAt(0);
                    }
                }
                field.setAccessible(true);
                field.set(t,value);
            }
        }
        return t;
    }
}
